package com.tnl.app.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.tnl.app.entity.TbFile;
import com.tnl.base.BaseDAO;
/**
 * 文件存储分外内，结构优化，暂时替代方案，由于liunx openoffice转换针对图片出现偏差很大，选择百度文档
 * @author trend
 *
 */

@Repository
public class TbFileDAO {
	private static final String SQL_GET_ID = "select * from tb_file  where id = ?";
	private static final String SQL_GET_DOCID="select * from tb_file where docId=?";
	private static final String SQL_GET_NAME = "select * from tb_file  where Name = ?";
	private static final String SQL_GET_ALL = "select * from tb_file ";
	protected static final String SQL_SAVE = "insert into tb_file(jobId,personId,docId,original_name,"
			+ "new_name,file_type,file_path,convert_path,relative_path,"
			+ "creat_time,update_time,create_by,update_by) "
			+ "values(?,?,?,?"
			+ ",?,?,?,?,?" + ",?,?,?,?)";
	
	protected static final String SQL_UPDATE = "update tb_file set jobId=?, personId=?"
			+ ", original_name=?,new_name=?,file_type =?,file_path=?,convert_path=?,relative_path=?,update_time=? "
			+ ", update_by=? where docId =?";
	
	private static final String SQL_UPDATE_PERSONBYDOCID="update tb_file set personId=? where docId=?";
	private static final String SQL_UPDATE_JOBBYDOCID="update tb_file set jobId=? where docId=?";
	private static final String SQL_GET_DOC_PERSONID="select * from tb_file where personId=? order by id desc limit 0,10";
	private static final String SQL_GET_DOC_PERSONID_LIMIT="select * from tb_file where personId=? order by id desc limit 0,1";
	private static final String SQL_GET_DOC_JOBID="select * from tb_file where jobId=? order by id desc limit 0,10";
	private static final String SQL_GET_FILE_JOBID="select * from tb_file where jobId=?";
	private static final String SQL_UPDATEFILE_JSONPARSE_PERSONID="update tb_file set jsonParse=? where personId=?";
	private static final String SQL_DEL_FILE = "delete from tb_file  where id=? and personId=?";
	public List<TbFile> getALL() {
		return BaseDAO.queryForList(TbFile.class, SQL_GET_ALL);
	}
	
	public TbFile getById(int Id) {
		return  BaseDAO.queryForObject(TbFile.class, SQL_GET_ID, Id);
	}
	
	public TbFile getByDocId(String docId) {
		return  BaseDAO.queryForObject(TbFile.class, SQL_GET_DOCID, docId);
	}
	
	public TbFile getFileByJobId(long jobId){
		return BaseDAO.queryForObject(TbFile.class, SQL_GET_FILE_JOBID, jobId);
	}
	
	public List<TbFile> getDocByPersonId(Integer personId){
		return BaseDAO.queryForList(TbFile.class, SQL_GET_DOC_PERSONID, personId);
	}
	
	public TbFile getDocByPersonIdLimit(Integer personId){
		return BaseDAO.queryForObject(TbFile.class, SQL_GET_DOC_PERSONID_LIMIT, personId);
	}
	
	
	public List<TbFile> getDocByJobId(long jobId){
		return BaseDAO.queryForList(TbFile.class, SQL_GET_DOC_JOBID, jobId);
	}
	
	static final String SQL_COUNT_DOC_PERSONID="select count(*) from tb_file where personId=? order by id desc ";
	public long getCountDocByPersonId(Integer personId){
		return (Long) BaseDAO.queryOneRowOneCol(SQL_COUNT_DOC_PERSONID, personId);
	}
	
	public TbFile getByName(String name){
		return  BaseDAO.queryForObject(TbFile.class, SQL_GET_NAME, name);
	}
	
	public int save(TbFile tbFile) {
		if (tbFile == null) {
			return -1;
		}
		Long id = BaseDAO.save(SQL_SAVE, tbFile.getJobId(), tbFile.getPersonId(),
				tbFile.getDocId(), tbFile.getOriginal_name(), tbFile.getNew_name(),
				tbFile.getFile_type(), tbFile.getFile_path(), tbFile.getConvert_path(),tbFile.getRelative_path(),
				tbFile.getCreat_time(), tbFile.getUpdate_time(), tbFile.getCreate_by(),
				tbFile.getUpdate_by());

		return id.intValue();
	}
	
	public int update(TbFile tbFile){
		return BaseDAO.execute(SQL_UPDATE,tbFile.getJobId(),tbFile.getPersonId(),tbFile.getOriginal_name(),
				tbFile.getNew_name(),tbFile.getFile_type(),tbFile.getFile_path(),tbFile.getConvert_path(),tbFile.getRelative_path(),
				tbFile.getUpdate_time(),tbFile.getUpdate_by());
	}
	
	
	public int updPersonIdByDocId(int personId,String docId){
		return BaseDAO.execute(SQL_UPDATE_PERSONBYDOCID, personId,docId);
	}
	
	
	public int updateFileByPersonId(int personId,String jsonParse){
		return BaseDAO.execute(SQL_UPDATEFILE_JSONPARSE_PERSONID,jsonParse,personId);
	}
	
	public int updJobIdByDocId(int jobId,String docId){
		return BaseDAO.execute(SQL_UPDATE_JOBBYDOCID, jobId,docId);
	}

	public void del(Integer id, int personId) {
		BaseDAO.execute(SQL_DEL_FILE,id,personId);
	}

	private final static String SQL_INSERT= "insert into tb_file(jobId,personId,docId,original_name,"
			+ "new_name,file_type,file_path,convert_path,relative_path,"
			+ "creat_time,update_time,create_by,update_by,studentId) "
			+ "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
	public void saveNEW(TbFile tbFile) {
		// TODO Auto-generated method stub
		BaseDAO.execute(SQL_INSERT, tbFile.getJobId(), tbFile.getPersonId(),
				tbFile.getDocId(), tbFile.getOriginal_name(), tbFile.getNew_name(),
				tbFile.getFile_type(), tbFile.getFile_path(), tbFile.getConvert_path(),tbFile.getRelative_path(),
				tbFile.getCreat_time(), tbFile.getUpdate_time(), tbFile.getCreate_by(),
				tbFile.getUpdate_by(),tbFile.getStudentId());
	}
}
